package com.umer.pinban.scan;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.common.bean.InfoResult;
import com.common.bean.MsgBean;
import com.common.bean.UserInfo;
import com.common.okhttp.OkEntityListRequest;
import com.common.okhttp.OkEntityRequest;
import com.common.ui.barlibrary.ImmersionBar;
import com.common.ui.recycleview.listener.OptListener;
import com.common.utils.AntiShake;
import com.common.utils.Constants;
import com.common.utils.EventConstants;
import com.common.utils.SpUtil;
import com.lxj.xpopup.XPopup;
import com.umer.pinban.AppHolder;
import com.umer.pinban.R;
import com.umer.pinban.base.BasicActivity;
import com.umer.pinban.bean.PersonnelList;
import com.umer.pinban.bean.customer.CommodityListBean;
import com.umer.pinban.bean.customer.ScanCustomerProjectList;
import com.umer.pinban.bean.customer.SelectCustomerInfo;
import com.umer.pinban.scan.adapter.ScanCustomerProjectJiaGouMaiAdapter;
import com.umer.pinban.scan.adapter.ScanCustomerProjectYiJingGouMaiAdapter;
import com.umer.pinban.scan.dialog.DialogSelectStaff;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;

public class XiaoFeiSelectActivity extends BasicActivity implements OptListener {

    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tvOne)
    TextView tvOne;
    @BindView(R.id.recyclerOne)
    RecyclerView recyclerOne;
    @BindView(R.id.tvTwo)
    TextView tvTwo;
    @BindView(R.id.recyclerTwo)
    RecyclerView recyclerTwo;
    @BindView(R.id.tv_jiesuan)
    TextView tvJiesuan;
    @BindView(R.id.tv_jiagou)
    TextView tvJiagou;
    private String shopId;
    private String openId;//顾客身份标识 扫码的时候传
    private String payCode;//交易码 扫码的时候传
    private String customerId  ="";
    private String customerName  ="";
    private UserInfo userInfo;
    private String balance = "";//账户剩余的余额
    private ScanCustomerProjectYiJingGouMaiAdapter scanCustomerProjectYiJingGouMaiAdapter;
    private List<ScanCustomerProjectList> listInfosOne = new ArrayList<>();//已经购买的项目

    //加购项目
    private ScanCustomerProjectJiaGouMaiAdapter scanCustomerProjectJiaGouMaiAdapter;
    private List<CommodityListBean> listInfosTwo = new ArrayList<>();//加购买的项目
    private List<CommodityListBean> listInfosTwoSelect = new ArrayList<>();//加购买的项目

    //优美师加上去的
    private int indexProject = -1;//加购项目索引值
    private String commodityId ="";//商品ID
    private String selectType = "customerNum";//查美容师顾客数量传：“customerNum”；查美容师工资统计：“wageStatistics”
    private String monthTime = "";
    private String jishiId = "";
    private String jishiName = "";
    private int customerStatus =1;
    private int experienceStatus = 0;
    private List<PersonnelList> personnelListList = new ArrayList<>();
    private DialogSelectStaff dialogSelectStaff;

    @Override
    public View isSupportLoadSirEnabled() {
        return null;
    }

    @Override
    public boolean isImmersionBarEnabled() {
        return true;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                //是否可以修改导航栏颜色，默认为true
                .navigationBarEnable(true)
                //导航栏颜色，不写默认黑色
                .navigationBarColor(R.color.barcolor)
                .statusBarColor(R.color.colorPrimary)
                //解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                .fitsSystemWindows(true)
                .init();
    }

    @Override
    public boolean isSupportSwipeBackEnabled() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_xiao_fei_select;
    }


    @OnClick({R.id.linear_back, R.id.tv_jiagou, R.id.tv_jiesuan})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                if (AntiShake.check(view.getId())) {
                    return;
                }
                finish();
                break;
            case R.id.tv_jiagou:
                if (AntiShake.check(view.getId())) {
                    return;
                }
                Intent intent = new Intent(this,SelectJiaGouProjectActivity.class);
                intent.putExtra("customerId",customerId);
                startActivity(intent);
                break;
            case R.id.tv_jiesuan:
                if (AntiShake.check(view.getId())) {
                    return;
                }
                selectCustomerInfo();
                break;
            default:
                break;
        }
    }

    @Override
    public void onOptClick(View view, Object object) {
        int index = (int) object;
        switch (view.getId()){
            case R.id.linear_select_jishi:
                indexProject = (int) object;
                commodityId = String.valueOf(scanCustomerProjectYiJingGouMaiAdapter.getDataSource().get(index).getCommodityId());
                if(personnelListList!=null && personnelListList.size()>0){
                    personnelListList.clear();
                }
                personnelList();
                break;
            default:break;
        }
    }

    @Override
    public void init() {
        SpUtil.putString(this,Constants.xiaofei,"");
        SpUtil.putString(this,Constants.xiaohao,"");
        dialogSelectStaff = new DialogSelectStaff(this);
        userInfo = AppHolder.getInstance().getUserInfo();
        openId = getIntent().getStringExtra("openId");
        payCode = getIntent().getStringExtra("payCode");
        scanCustomerProjectYiJingGouMaiAdapter = new ScanCustomerProjectYiJingGouMaiAdapter(this,listInfosOne,
                R.layout.activity_scan_customer_project_list_yijing_goumai_item,this);
        LinearLayoutManager linearLayoutManagerOne = new LinearLayoutManager(this);
        recyclerOne.setLayoutManager(linearLayoutManagerOne);
        recyclerOne.setAdapter(scanCustomerProjectYiJingGouMaiAdapter);

        scanCustomerProjectJiaGouMaiAdapter = new ScanCustomerProjectJiaGouMaiAdapter(this,listInfosTwoSelect,
                R.layout.activity_scan_customer_project_list_jia_goumai_item,this);
        LinearLayoutManager linearLayoutManagerTwo = new LinearLayoutManager(this);
        recyclerTwo.setLayoutManager(linearLayoutManagerTwo);
        recyclerTwo.setAdapter(scanCustomerProjectJiaGouMaiAdapter);
    }

    @Override
    public void loadData() {
        selectCustomerInfo();
    }

    @Override
    public void handlerMeg(MsgBean msgBean) {
        super.handlerMeg(msgBean);
        int flag = msgBean.getFlag();
        if(flag == EventConstants.jiagou_project){
            listInfosTwo = (List<CommodityListBean>) msgBean.getObject();
            if(listInfosTwoSelect.size()>0){
                listInfosTwoSelect.clear();
            }
            for(int i=0;i<listInfosTwo.size();i++){
                int count = listInfosTwo.get(i).getCount();
                for(int j=0;j<count;j++){
                    CommodityListBean commodityListBean = new CommodityListBean();
                    commodityListBean.setPrice(listInfosTwo.get(i).getPrice());
                    commodityListBean.setCount2(listInfosTwo.get(i).getCount2());
                    commodityListBean.setCount(listInfosTwo.get(i).getCount());
                    commodityListBean.setApproveStatus(listInfosTwo.get(i).getApproveStatus());
                    commodityListBean.setCommodityId(listInfosTwo.get(i).getCommodityId());
                    commodityListBean.setCreateDate(listInfosTwo.get(i).getCreateDate());
                    commodityListBean.setCreateUser(listInfosTwo.get(i).getCreateUser());
                    commodityListBean.setFilePath(listInfosTwo.get(i).getFilePath());
                    commodityListBean.setFileUuid(listInfosTwo.get(i).getFileUuid());
                    commodityListBean.setId(listInfosTwo.get(i).getId());
                    commodityListBean.setName(listInfosTwo.get(i).getName());
                    commodityListBean.setMemberCardId(listInfosTwo.get(i).getMemberCardId());
                    commodityListBean.setZhicheng(listInfosTwo.get(i).getZhicheng());
                    commodityListBean.setMemberCardName(listInfosTwo.get(i).getMemberCardName());
                    commodityListBean.setType(listInfosTwo.get(i).getType());
                    listInfosTwoSelect.add(commodityListBean);
                }
            }
            scanCustomerProjectJiaGouMaiAdapter.setDataSource(listInfosTwoSelect);
        }
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {
        switch (what) {
            case R.id.scanCustomerProjectList:
                listInfosOne = (List<ScanCustomerProjectList>) t.getT();
                scanCustomerProjectYiJingGouMaiAdapter.setDataSource(listInfosOne);
               /* setLook();
                setState();*/
                break;
            case R.id.selectCustomerInfo:
                SelectCustomerInfo selectCustomerInfo = (SelectCustomerInfo) t.getT();
                if(selectCustomerInfo!=null){
                    customerId = selectCustomerInfo.getCustomerId();
                    customerName = selectCustomerInfo.getName();
                    balance = selectCustomerInfo.getBalance();
                    customerStatus = selectCustomerInfo.getCustomerStatus();
                    experienceStatus = selectCustomerInfo.getExperienceStatus();
                }
                scanCustomerProjectList();
                break;
            case R.id.personnelList:
                personnelListList = (List<PersonnelList>) t.getT();
                setStaff();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {

    }

    private void setStaff() {
        dialogSelectStaff.setPersonnelListList(personnelListList);
        new XPopup.Builder(this)
                .asCustom(dialogSelectStaff)
                .show();
    }

    /**
     * 查询美容师列表
     */
    private void personnelList(){
        OkEntityListRequest<PersonnelList> request = new OkEntityListRequest<>(R.id.personnelList,
                Constants.personnelList, PersonnelList.class);
        request.addParams("shopId", userInfo.getShopId());
        request.addParams("commodityId", commodityId);
        request.addParams("customerId",customerId);
        request.addParams("monthTime",monthTime);
        request.addParams("selectType",selectType);
        request.addParams("customerStatus",customerStatus);
        request.addParams("experienceStatus",experienceStatus);
        request.setHeader(true);
        requestOkhttpEntityList(request);
    }

    /**
     * 查询顾客信息
     */
    private void selectCustomerInfo(){
        OkEntityRequest<SelectCustomerInfo> request = new OkEntityRequest<>(R.id.selectCustomerInfo,
                Constants.selectCustomerInfo, SelectCustomerInfo.class);
        request.addParams("payCode", payCode);
        request.addParams("openId", openId);
        request.addParams("shopId", userInfo.getShopId());
        //request.addParams("customerId", customerId);
        request.setHeader(true);
        requestOkhttpEntity(request);
    }

    /**
     * 获取顾客项目列表
     */
    private void scanCustomerProjectList() {
        OkEntityListRequest<ScanCustomerProjectList> request =
                new OkEntityListRequest<>(R.id.scanCustomerProjectList,
                        Constants.scanCustomerProjectList, ScanCustomerProjectList.class);
        request.addParams("shopId", shopId);
        request.addParams("openId", openId);
        request.addParams("payCode", payCode);
        //request.addParams("customerId", customerId);
        request.setHeader(true);
        requestOkhttpEntityList(request);
    }
}
